var settings = settings ||{};
var pageContext = new PageContextDatatable(settings||{});

pageContext.formInit = function(layero,index,type){
} 
pageContext.resetAfter = function (){
	 
}	 
pageContext.getOtherOperBtn = function(id,label,record){
	var html = [];	
	if(pageContext.setting.canShowRole){
		html.push('<li>');
		html.push('<a href="javacript:void(0)" label="'+label+'" url="'+window.global.getFinalUrl(window.global.ctx+'/auth/userrole/toedit?userId='+record.id)+'" instance="'+record.instanceId+'"  val="'+id+'" class="tooltip-success propBtn" data-rel="tooltip" title="角色">');
		html.push('<span class="green">');
		html.push('<i class="ace-icon fa fa-wrench bigger-120"></i>');
		html.push('</span>');
		html.push('</a>');
		html.push('</li>');
	}
	if(pageContext.setting.canShowGroup){
		html.push('<li>');
		html.push('<a href="javacript:void(0)" label="'+label+'" url="'+window.global.getFinalUrl(window.global.ctx+'/auth/usergroup/toedit?userId='+record.id)+'" instance="'+record.instanceId+'"  val="'+id+'" class="tooltip-success groupBtn" data-rel="tooltip" title="用户组">');
		html.push('<span class="green">');
		html.push('<i class="ace-icon fa fa-flask bigger-120"></i>');
		html.push('</span>');
		html.push('</a>');
		html.push('</li>');
	}
	if(pageContext.setting.activation){
		html.push('<li>');
		html.push('<a href="javacript:void(0)" label="'+label+'"  val="'+id+'" class="tooltip-success activationBtn" data-rel="tooltip" title="发送激活邮件">');
		html.push('<span class="green">');
		html.push('<i class="ace-icon fa fa-cog bigger-120"></i>');
		html.push('</span>');
		html.push('</a>');
		html.push('</li>');
	}
	if(pageContext.setting.person){
		html.push('<li>');
		html.push('<a href="javacript:void(0)" label="'+label+'" url="'+window.global.getFinalUrl(window.global.ctx+'/auth/account/person?id='+record.personId+'&userId='+record.id)+'" val="'+id+'" class="tooltip-success personBtn" data-rel="tooltip" title="个人基本信息">');
		html.push('<span class="green">');
		html.push('<i class="ace-icon fa fa-flask bigger-120"></i>');
		html.push('</span>');
		html.push('</a>');
		html.push('</li>');
	}
	return html.join("");
}
pageContext.getOtherPingPuOperBtn = function(id,label,record){
	var html = [];
	if(pageContext.setting.canShowRole){
		html.push('<button class="btn btn-xs btn-info propBtn" url="'+window.global.getFinalUrl(window.global.ctx+'/auth/userrole/toedit?userId='+record.id)+'" label="'+label+'" instance="'+record.instanceId+'"  val="'+id+'" title="角色">');
		html.push('<i class="ace-icon fa fa-wrench bigger-120"></i>');
		html.push('</button>');
	}
	if(pageContext.setting.canShowGroup){
		html.push('<button class="btn btn-xs btn-info groupBtn" url="'+window.global.getFinalUrl(window.global.ctx+'/auth/usergroup/toedit?userId='+record.id)+'" label="'+label+'" instance="'+record.instanceId+'"  val="'+id+'" title="用户组">');
		html.push('<i class="ace-icon fa fa-cog bigger-120"></i>');
		html.push('</button>');
	}
	if(pageContext.setting.activation){
		html.push('<button class="btn btn-xs btn-info activationBtn"  label="'+label+'" val="'+id+'" title="发送激活邮件">');
		html.push('<i class="ace-icon fa fa-flask bigger-120"></i>');
		html.push('</button>');
	}
	if(pageContext.setting.person){
		html.push('<button class="btn btn-xs btn-info personBtn"  url="'+window.global.getFinalUrl(window.global.ctx+'/auth/account/person?id='+record.personId+'&userId='+record.id)+'"  label="'+label+'" val="'+id+'" title="个人基本信息">');
		html.push('<i class="ace-icon fa fa-flask bigger-120"></i>');
		html.push('</button>');
	}
	return html.join("");
}

pageContext.renderAfterEventOther = function(){
	$('#'+pageContext.setting.tableId+' >tbody').on("click","tr > td .propBtn",function(e){
		 e.stopPropagation();
		 var val = $(this).attr("val");
		 var label = $(this).attr("label");
		 var instance = $(this).attr("instance");
		 var url = ($(this).attr("url"));
		 url = window.global.addPara(url,"instanceId",instance,true);
		 load(url,"page-content",true,"用户【"+label+"】"+"角色",function(){
				
		 });
	     return false;
	});
	$('#'+pageContext.setting.tableId+' >tbody').on("click","tr > td .groupBtn",function(e){
		e.stopPropagation();
		 var val = $(this).attr("val");
		 var label = $(this).attr("label");
		 var instance = $(this).attr("instance");
		 var url = ($(this).attr("url"));
		 url = window.global.addPara(url,"instanceId",instance,true);
		 load(url,"page-content",true,"用户【"+label+"】所在"+"用户组",function(){
				
		 });
	     return false;
	});
	$('#'+pageContext.setting.tableId+' >tbody').on("click","tr > td .activationBtn",function(e){
		e.stopPropagation();
		 var val = $(this).attr("val");
		 var label = $(this).attr("label");
		 
	     return false;
	});
	$('#'+pageContext.setting.tableId+' >tbody').on("click","tr > td .personBtn",function(e){
		e.stopPropagation();
		  var val = $(this).attr("val");
		 var label = $(this).attr("label");
		 var url = ($(this).attr("url"));
		 load(url,"page-content",true,"用户【"+label+"】"+"基本信息",function(){
				
		 });
	     return false;
	});
}

var searchTreesetting = {
		view: {
			dblClickExpand: false
		},
		data: {
			simpleData: {
				enable: true
			}
		},
		callback: {
			 onClick: onInputTreeClickSearch
		}
}; 

var treeSetting = {
		dialog:false
		,onInputTreeBodyDown:onInputTreeBodyDown
		,target:$("#searchForm").find("input[name='orgId']").next("input")
}
var pageContextInputTree = new PageContextInputTree(treeSetting);

function onInputTreeClickSearch(e, treeId, treeNode) {
	pageContextInputTree.onClickSearch(e, treeId, treeNode);
}

function onInputTreeBodyDown(event) {
	
	var node = $(event.target).closest("li").attr("treenode");
	if (!($(event.target).attr("treehide") || node=="")) {
		pageContextInputTree.hideMenu();
	}
}
pageContext.toAdd = function(option){
	var action = window.global.getFinalUrl(pageContext.setting.toAddUrl);
	load(action,"page-content",true,"新增"+pageContext.modelName,function(){
		
	});
}
pageContext.toDetail = function(id,data){
	var action = window.global.getFinalUrl(pageContext.setting.toDetailUrl);
	action = window.global.addPara(action,"id",id,true)
	load(action,"page-content",true,pageContext.modelName+"详情",function(){
		
	});
}
pageContext.toUpdate = function(id,data){
	var action = window.global.getFinalUrl(pageContext.setting.toUpdateUrl);
	action = window.global.addPara(action,"id",id,true)
	load(action,"page-content",true,"修改"+pageContext.modelName,function(){
		
	});
}
jQuery(function($) {
	pageContext.init();
	 $(document).find("input").each(function(){
		 $(this).attr("autocomplete","off");
	 });
   $("#searchForm .inputElem").mailAutoComplete();
   pageContext.get({},{
	   url:window.global.ctx+"/sys/org/query?noPage=1",
	   success:function(result,para){
   			var data = result.data;
   			if(data){ 
   				var list = data.list;
   				pageContextInputTree.initTree($($("#searchForm").find(".ztree")[0]), list,searchTreesetting);
   			}
	   }
   });
   var layero = $("#searchForm");
	layero.find('select[novalid!="true"]').each(function(){
		var novalid = $(this).attr("noconvert");
		if(novalid!="true"){
			var $span = layero.find(this).select2({
	            language: "zh-CN"
		            ,matcher:select2Matcher})
	            .next("span")
	            ;
		}
	  });
}); 
